﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web.Http;
using CQIE.TES.IService;
using CQIE.TES.Model;
using CQIE.TES.WEB.USER.Models;
using Newtonsoft.Json.Linq;

namespace CQIE.TES.WEB.USER.Controllers.Eva
{
    public class EvaluatingAPIController : ApiController
    {
        public IEvaluaHeightService EvaluaHeightService { get; set; }
        public ICourseService CourseService { get; set; }
        public IRecordService RecordService { get; set; }
        

        [HttpGet]
        [Route("api/course/getcourse")]
        public object GetCourse(int studentId,int evaId)
        {
            var list = CourseService.GetCourseByStudentID(studentId, evaId);
            var data = list.Select(r => new
            {
                id = r.ID,
                name = r.Name,
                teacherId = r.Teacher.ID,
                teacherName = r.Teacher.Name
            });
            
            return data;
        }

        /// <summary>
        /// 学生是否已经参与过评测
        /// </summary>
        /// <param name="evaid"></param>
        /// <param name="teacherid"></param>
        /// <param name="courseId"></param>
        /// <param name="sid"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/eva/isrecorded")]
        public object IsRecorded(int evaid,int teacherid,int courseId,int sid)
        {
            var list = RecordService.IsRecorded(evaid, teacherid, courseId, sid);
            if (list.Count==0)
            {
                return null;
            }

            var data = list[0].TheLog;  
            data = Regex.Replace(data, @"\\r\\n", "");
            return data;
        }

        /// <summary>
        /// 系部领导是否已投票
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [Route("api/eva/departmentisrecorded")]
        public object DepartmentIsRecorded(int evaId,int teacherId,int aimTeacherId)
        {
            var list = RecordService.IsRecorded(evaId, teacherId, aimTeacherId,ENUM_EVA_TYPE.Department);
            if (list.Count == 0)
            {
                return null;
            }

            return list.FirstOrDefault()?.TheLog;
        }

        [HttpGet]
        [Route("api/eva/teacherofficeisrecorded")]
        public object TeacherOfficeIsRecorded(int evaId, int teacherId, int aimTeacherId)
        {
            var list = RecordService.IsRecorded(evaId, teacherId, aimTeacherId, ENUM_EVA_TYPE.CourseLeader);
            if (list.Count == 0)
            {
                return null;
            }

            return list.FirstOrDefault()?.TheLog;
        }

        /// <summary>
        /// 教师自评是否全部投票
        /// </summary>
        /// <param name="evaId"></param>
        /// <param name="teacherId"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/eva/teacherselfisrecorded")]
        public object TeacherSelfIsRecorded(int evaId,int teacherId)
        {
            var list = RecordService.IsRecorded(evaId, teacherId);
            if (list.Count==0)
            {
                return null;
            }
            return list.FirstOrDefault()?.TheLog;
        }
        /// <summary>
        /// 判断教师互评是否全部完成投票
        /// </summary>
        /// <param name="evaId"></param>
        /// <param name="teacherId"></param>
        /// <param name="aimTeacherId"></param>
        /// <returns></returns>

        [HttpGet]
        [Route("api/eva/teacherotherisrecorded")]
        public object TeacherOtherIsRecorded(int evaId,int teacherId,int aimTeacherId)
        {
            var list = RecordService.IsRecorded(evaId, teacherId, aimTeacherId,ENUM_EVA_TYPE.Teacher);
            if (list.Count == 0)
            {
                return null;
            }

            return list.FirstOrDefault()?.TheLog;
        }

        /// <summary>
        /// 学生评教
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/eva/submit")]
        public object GetScore([FromBody]JObject model)
        {
            // 获取数据
            int evaId = model.Value<int>("evaid");
            int teacherid = model.Value<int>("teacherid");
            int courseId = model.Value<int>("courseId");
            int studentId = model.Value<int>("sid");
            List<int> answerList = model.Value<JArray>("answerIds").ToObject<List<int>>();
            List<int> question = model.Value<JArray>("questionIds").ToObject<List<int>>();

            RecordService.Record(evaId, teacherid, courseId, studentId, answerList, question);
            return null;
        }

        [HttpPost]
        [Route("api/eva/department/submit")]
        public object DepartmentSubmit([FromBody]JObject model)
        {
            int evaId = model.Value<int>("evaId");
            int teacherId = model.Value<int>("teacherId");
            int aimTeacherId = model.Value<int>("aimTeacherId");

            List<int> answerList = model.Value<JArray>("answerIds").ToObject<List<int>>();
            List<int> questionList = model.Value<JArray>("questionIds").ToObject<List<int>>();

            bool isSuccess = RecordService.Record(evaId, teacherId, aimTeacherId,answerList, questionList,ENUM_EVA_TYPE.Department);
            return isSuccess;
        }

        /// <summary>
        /// 教研室评教
        /// </summary>
        /// <param name="model"></param>
        /// <param name="department"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/eva/teachoffice/submit")]
        public object TeacheOfficeSubmit([FromBody] JObject model)
        {
            int evaId = model.Value<int>("evaId");
            int teacherId = model.Value<int>("teacherId");
            int aimTeacherId = model.Value<int>("aimTeacherId");

            List<int> answerList = model.Value<JArray>("answerIds").ToObject<List<int>>();
            List<int> questionList = model.Value<JArray>("questionIds").ToObject<List<int>>();

            bool isSuccess = RecordService.Record(evaId, teacherId, aimTeacherId, answerList, questionList, ENUM_EVA_TYPE.CourseLeader);
            return isSuccess;
        }

        [HttpPost]
        [Route("api/eva/teacherotherisrecorded/submit")]
        public object TeacherOtherRecorded([FromBody] JObject model) 
        {
            int evaId = model.Value<int>("evaId");
            int teacherId = model.Value<int>("teacherId");
            int aimTeacherId = model.Value<int>("aimTeacherId");

            List<int> answerList = model.Value<JArray>("answerIds").ToObject<List<int>>();
            List<int> questionList = model.Value<JArray>("questionIds").ToObject<List<int>>();

            bool isSuccess = RecordService.TeacherOtherRecord(evaId, teacherId, aimTeacherId, answerList, questionList);
            return isSuccess;

        }

        /// <summary>
        /// 教师自评提交保存提交结果
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/eva/teacherself/submit")]
        public object TeacherSelfSubmit([FromBody]JObject model)
        {
            int evaId = model.Value<int>("evaId");
            int teacherId = model.Value<int>("teacherId");
            List<int> answerList = model.Value<JArray>("answerIds").ToObject<List<int>>();
            List<int> questionList = model.Value<JArray>("questionIds").ToObject<List<int>>();
            //保存提交结果
            bool isSuccess = RecordService.Record(evaId, teacherId, answerList, questionList);
            return isSuccess;
        }
    }
}
